package com.leetcode.algorithm.y22.m09.w4;

/**
 * 1544. 整理字符串
 * 
 * https://leetcode.cn/problems/make-the-string-great/
 * 
 * @author jie.deng
 *
 */
class Question1544Solution01 {

	public String makeGood(String s) {
		char[] chs = s.toCharArray();
		int nextIdx = 0;
		for (int i = 0; i < chs.length; i++) {
			char invalidCh = ' ';
			if (nextIdx >= 1) {
				if (chs[nextIdx - 1] < 'a') {
					invalidCh = (char) (chs[nextIdx - 1] + 32);
				} else {
					invalidCh = (char) (chs[nextIdx - 1] - 32);
				}
			}
			if (invalidCh == chs[i]) {
				nextIdx--;
			} else {
				chs[nextIdx++] = chs[i];
			}
		}
		return new String(chs, 0, nextIdx);
	}

}